package com.lrkj.lease.service.impl;

import java.util.List;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.StringUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.lease.service.IFirstPartyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lrkj.lease.mapper.ContractCategoryMapper;
import com.lrkj.lease.domain.ContractCategory;
import com.lrkj.lease.service.IContractCategoryService;

/**
 * 合同类型Service业务层处理
 *
 * @author yajun_ren
 * @date 2022-04-26
 */
@Service
public class ContractCategoryServiceImpl implements IContractCategoryService
{
    @Autowired
    private ContractCategoryMapper contractCategoryMapper;

    @Autowired
    private IFirstPartyService firstPartyService;

    /**
     * 查询合同类型
     *
     * @param id 合同类型ID
     * @return 合同类型
     */
    @Override
    public ContractCategory selectContractCategoryById(String id)
    {
        return contractCategoryMapper.selectContractCategoryById(id);
    }

    /**
     * 查询合同类型列表
     *
     * @param contractCategory 合同类型
     * @return 合同类型
     */
    @Override
    public List<ContractCategory> selectContractCategoryList(ContractCategory contractCategory)
    {
        return contractCategoryMapper.selectContractCategoryList(contractCategory);
    }

    /**
     * 新增合同类型
     *
     * @param contractCategory 合同类型
     * @return 结果
     */
    @Override
    public int insertContractCategory(ContractCategory contractCategory)
    {
        contractCategory.setId(IdUtils.simpleUUID());
        contractCategory.setCreateTime(DateUtils.getNowDate());
        this.setInfo(contractCategory);
        return contractCategoryMapper.insertContractCategory(contractCategory);
    }

    /**
     * 修改合同类型
     *
     * @param contractCategory 合同类型
     * @return 结果
     */
    @Override
    public int updateContractCategory(ContractCategory contractCategory)
    {
        contractCategory.setUpdateTime(DateUtils.getNowDate());
        this.setInfo(contractCategory);
        return contractCategoryMapper.updateContractCategory(contractCategory);
    }

    public void setInfo(ContractCategory contractCategory){
        if(null != contractCategory.getFirsPartyList() && contractCategory.getFirsPartyList().size() > 0){
            contractCategory.getFirsPartyList().stream().forEach(x->{
                x.setContractCategoryId(contractCategory.getId());
                if(StringUtils.isNotEmpty(x.getId())){
                    firstPartyService.updateFirstParty(x);
                }else{
                    firstPartyService.insertFirstParty(x);
                }
            });
        }
    }

    /**
     * 批量删除合同类型
     *
     * @param ids 需要删除的合同类型ID
     * @return 结果
     */
    @Override
    public int deleteContractCategoryByIds(String[] ids)
    {
        return contractCategoryMapper.deleteContractCategoryByIds(ids);
    }

    /**
     * 删除合同类型信息
     *
     * @param id 合同类型ID
     * @return 结果
     */
    @Override
    public int deleteContractCategoryById(String id)
    {
        return contractCategoryMapper.deleteContractCategoryById(id);
    }
    /**
     * 批量新增合同类型信息
     *
     * @param contractCategorys 合同类型对象
     */
    public int batchAddData(List<ContractCategory> contractCategorys)
    {
           return contractCategoryMapper.batchAddData(contractCategorys);
    }
}
